package com.jrx.anytxn.customer.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.customer.constant.CustomerConstant;
import com.jrx.anytxn.customer.dto.customer.CustomerRes;
import com.jrx.anytxn.customer.entity.CmCustomerRightInfo;
import com.jrx.anytxn.customer.entity.TlCouponDetail;
import com.jrx.anytxn.customer.service.ICouponMeaasgeService;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.entity.PrRightsAndInterests;
import com.jrx.anytxn.param.entity.TlCouponSummary;
import com.jrx.anytxn.param.service.couponReport.ITlCouponSummaryService;

/**
 * 优惠券汇总明细信息相关处理类
 */
@Service
public class CouponMeaasgeService implements ICouponMeaasgeService {

    private static final Logger logger = LoggerFactory.getLogger(CouponMeaasgeService.class);


    @Autowired
    private ITlCouponSummaryService tlCouponSummaryService;
    @Autowired
    private ISegmentService iSegmentService;

    @Override
    public void buildCouponSummary(Long sumId ,String tenantId, String orgId,String fileName,int number,String uploadStatus ,String userName) throws TxnException {
        TlCouponSummary tlCouponSummary = new TlCouponSummary();

        if(0L == sumId){
            try {
                tlCouponSummary.setId(iSegmentService.getId(CustomerConstant.TL_COUPON_SUMMARY_INFO));
            } catch (SegmentException e) {
                logger.error("主键生成错误",e);
                throw new TxnException(ErrorMsgConstant.ERROR_ID);
            }
        }else{
            tlCouponSummary.setId(sumId);
        }
        tlCouponSummary.setTenantId(tenantId);
        tlCouponSummary.setOrganizationId(orgId);
        tlCouponSummary.setTransactionsNumber(number);
        tlCouponSummary.setFileName(fileName);
        tlCouponSummary.setUploadStatus(uploadStatus);
        tlCouponSummary.setUpdateBy(userName);
        tlCouponSummary.setRecordVersionNumber(CustomerConstant.INIT_RECORD_VERSION_NUMBER);

        tlCouponSummaryService.addCouponSummary(tlCouponSummary);
    }

    @Override
    public TlCouponDetail buildCouponDetail(CmCustomerRightInfo cmCustomerRightInfo,CustomerRes customerRes,PrRightsAndInterests rightsAndInterests,Long summaryId) throws TxnException {
        TlCouponDetail detail = new TlCouponDetail();
        try {
            detail.setId(iSegmentService.getId(CustomerConstant.TL_COUPON_DETAIL_INFO));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnException(ErrorMsgConstant.ERROR_ID);
        }
        detail.setTenantId(cmCustomerRightInfo.getTenantId());
        detail.setOrganizationId(cmCustomerRightInfo.getOrganizationId());
        detail.setChannel(cmCustomerRightInfo.getChannel());
        detail.setCustomerId(cmCustomerRightInfo.getCustomerId());
        detail.setChineseName(customerRes.getFirstLevel().getChineseName());
        detail.setIdNumber(customerRes.getFirstLevel().getIdNumber());
        detail.setMobile(customerRes.getSecondLevel().getMobile());
        detail.setRightsId(rightsAndInterests.getRightsId());
        detail.setRightsName(rightsAndInterests.getRightsName());
        detail.setRightsType(rightsAndInterests.getRightsType());
        detail.setCustomerRightsId(cmCustomerRightInfo.getId());
        detail.setCouponSummaryId(summaryId);
        detail.setUpdateBy(cmCustomerRightInfo.getUpdateBy());
        detail.setRecordVersionNumber(cmCustomerRightInfo.getRecordVersionNumber());

        return detail;
    }
}
